﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.Web.Controllers
{
    public class UserAddressController : UserController
    {
        public int page=1;         //当前页码
        public int totalcount=0;   //OUT数据总数
        public IActionResult Index()
        {
            int.TryParse(Request.Query["page"], out page);
            page = page > 0 ? page : 1;
            ViewBag.Page = page;
            string strwhere = "where user_id=" + GetUserInfo().id;
            var list = _user_addr_book.GetList(page, 50, strwhere, "add_time desc,id desc");
            totalcount = _user_addr_book.RecordCount(strwhere);
            string pageUrl = Utils.CombUrlTxt("/Web/Useraddress/Index", "page={0}", "__id__");
            ViewBag.pagelist = Utils.OutPageList(50, page, totalcount, pageUrl, 8);
            return View(list);
        }
        public string action;
        public User_addr_book model = new User_addr_book();
        public IUser_addr_bookService _user_addr_book { get; set; }
        public IActionResult Edit(int id)
        {
            action = Request.Query["act"];
            if (string.IsNullOrEmpty(action))
                action = "edit";
            if (action.ToLower() == CMSEnums.ActionEnum.Edit.ToString().ToLower())
            {
                if (!_user_addr_book.Exists(id))
                {
                    return ToError("出错了，您要浏览的页面不存在或已删除！");
                }
                model = _user_addr_book.GetModel(id);
                if (model.user_id != userModel.id)
                {
                    return ToError("出错了，您所要修改的并非自己的地址！");
                }
            }
            ViewBag.action = action.ToLower();
            ViewBag.ID = id;
            return View(model);
        }
    }
}